package com.sakura.probe.controller.admin;

import com.sakura.probe.common.api.CommonResult;
import com.sakura.probe.common.api.Page;
import com.sakura.probe.common.util.HttpServletRequestUtil;
import com.sakura.probe.pojo.PersonInfo;
import com.sakura.probe.pojo.Topic;
import com.sakura.probe.service.TopicCategoryService;
import com.sakura.probe.service.TopicService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "AdminTopicController",description = "后台话题的相关操作")
@Controller
@Slf4j
@RequestMapping("/backstage/topic")
public class AdminTopicController {
    @Autowired
    private TopicService topicService;
    @Autowired
    private TopicCategoryService categoryService;

    /**
     *
     * @param pageIndex 页码
     * @param pageSize 一页显示的页数
     * @param deleted 逻辑删除 1-将其回收至垃圾箱 0-正常
     * @param head null-显示所有 0-显示正常的话题 1-只显示轮播图话题
     * @param topicName 话题的标题
     * @param username 要搜索的话题题主
     * @param topicCategoryId 话题类型id
     * @return
     */
    @ApiOperation("根据相关条件获取话题列表")
    @GetMapping("/listByCondition")
    @ResponseBody
    @RequiresRoles(logical = Logical.OR,value = {"1","3"})
    public CommonResult getTopicList(Integer pageIndex,Integer pageSize,Integer deleted,Integer head,
                                     String topicName,String username,String topicCategoryId){
        Topic condition = combined(topicName,username,topicCategoryId,deleted,head);
        List<Topic> records = topicService.listByCondition(condition, pageIndex, pageSize).getData();
        //总数
        Long total = topicService.countByCondition(condition);
        //总页数
        Long pages = total%pageSize==0?total/pageSize:(total/pageSize+1);
        Page<Topic> page = new Page<>(records,total,pages);
        return CommonResult.success(page);
    }


    @ApiOperation("修改话题的状态值")
    @GetMapping("/updateStatus")
    @ResponseBody
    public CommonResult updateTopicStatus(Long topicId,Integer status){
        Topic topic = new Topic();
        topic.setTopicId(topicId);
        topic.setEnableStatus(status);
        boolean result = topicService.updateById(topic);
        return result?CommonResult.success("update success"):CommonResult.failed();
    }

    @ApiOperation("删除单个话题")
    @GetMapping("/delTopic")
    @ResponseBody
    public CommonResult delTopic(Long topicId,boolean type){
        boolean result = false;
        if (type){
            result = topicService.deleteTopic(topicId);
        }else{
            //逻辑删除(即只修改deleted而已)
            result = topicService.removeById(topicId);
        }
        return result?CommonResult.success("删除成功"):CommonResult.failed();
    }

    @ApiOperation("删除多个话题")
    @GetMapping("/delBatchTopic")
    @ResponseBody
    public CommonResult delBatchTopic(HttpServletRequest request,boolean type){
        List<Long> list = HttpServletRequestUtil.getLongList(request, "id", 10);
        boolean result = false;
        if (type){
            result = topicService.batchDeleteTopic(list);
        }else {
            //逻辑删除(即只修改deleted而已)
            result = topicService.removeByIds(list);
        }
        return result?CommonResult.success("批量删除成功"):CommonResult.failed();
    }

    @ApiOperation("更新优先级")
    @GetMapping("/updatePriority")
    @ResponseBody
    public CommonResult updatePriority(Long topicId,Integer priority){
        Topic topic = new Topic();
        topic.setTopicId(topicId).setPriority(priority);
        return topicService.updateById(topic)?CommonResult.success("修改成功"):CommonResult.failed("修改失败");
    }

    @ApiOperation("还原回收站中的话题")
    @GetMapping("/restore")
    @ResponseBody
    public CommonResult restoreTopic(Long topicId){
        return topicService.restoreTopic(topicId)?CommonResult.success("还原成功"):CommonResult.failed("还原失败");
    }

    @ApiOperation("批量还原回收站中的话题")
    @GetMapping("/restoreBatch")
    @ResponseBody
    public CommonResult restoreBatchTopic(HttpServletRequest request){
        List<Long> list = HttpServletRequestUtil.getLongList(request, "id", 10);
        return topicService.batchRestoreTopic(list)?CommonResult.success("批量还原成功"):CommonResult.failed("批量还原失败");
    }


    @ApiOperation("初始化话题选项")
    @GetMapping("/showTopicCategory")
    @ResponseBody
    public CommonResult initTopicCategory(){
        return CommonResult.success(categoryService.list(null));
    }



    /**
     * 组合查询条件
     */
    private Topic combined(String topicName, String username, String topicCategoryId, Integer deleted, Integer head) {
        Topic result = new Topic();
        if (null!=topicName&&!"".equals(topicName)){
            result.setTopicName(topicName);
        }
        if (null!=username&&!"".equals(username)){
            PersonInfo person = new PersonInfo();
            person.setUsername(username);
            result.setPersonInfo(person);
        }
        if (null!=topicCategoryId&&!"".equals(topicCategoryId)&&!"0".equals(topicCategoryId)){
            result.setTopicCategoryId(Long.valueOf(topicCategoryId));
        }
        if (null!=deleted){
            result.setDeleted(deleted);
        }
        if (null!=head){
            result.setEnableHeadline(head);
        }
        return result;
    }


}
